package com.shd.edanpei.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.TranBarActivity;
import com.shd.edanpei.base.dialog.DialogUtils;
import com.shd.edanpei.citypicker.utils.StringUtils;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.presenter.contract.LoginContract;
import com.shd.edanpei.enums.UrlEnum;
import com.shd.edanpei.presenter.LoginPresenter;
import com.shd.edanpei.util.AcCollector;
import com.shd.edanpei.util.LoginUtils;
import com.shd.edanpei.util.ToastUtil;
import com.shd.edanpei.view.ViewEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends TranBarActivity<LoginPresenter>
        implements LoginContract.LoginIView {


    /*—————————— TAB—————————— */
    @BindView(R.id.rg_login_tab)
    RadioGroup radioGroup;
    @BindView(R.id.rb_login)
    RadioButton rbLogin;
    @BindView(R.id.rb_signup)
    RadioButton rbSignUp;
    @BindView(R.id.tv_login_argeement)
    TextView tv_login_argeement;

    /*—————————— 登录页—————————— */
    @BindView(R.id.et_login_account)
    EditText etAccount;
    @BindView(R.id.et_login_password)
    EditText etPassword;
//    @BindView(R.id.tv_login_argeement)
//    TextView tvAgreement;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_login_forget)
    TextView tvForget;
    @BindView(R.id.et_login_code)
    EditText etLoginCode;
    @BindView(R.id.tv_login_getcode)
    TextView tv_login_getcode;
    @BindView(R.id.tv_codelogin_passLogin)
    TextView tv_codelogin_passLogin;
    @BindView(R.id.rl_login_code)
    RelativeLayout rl_login_code;

    /*—————————— 注册页—————————— */
    @BindView(R.id.tv_signup_getcode)
    TextView tvCAPTCHA;
    @BindView(R.id.et_signup_account)
    ViewEditText etSignUpAccount;
    @BindView(R.id.et_signup_code)
    EditText etSignUpCode;
    @BindView(R.id.et_signup_password1)
    ViewEditText etSignUpPassword1;
    @BindView(R.id.et_signup_password2)
    ViewEditText etSignUpPassword2;


    @BindView(R.id.child_login_layout)
    View viewLogin;
    @BindView(R.id.child_signup_layout)
    View viewSignup;

    //当前布局id
    private int mLayoutId;
    private  int loginType=1;

    @Override
    protected int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected LoginPresenter bindPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        AcCollector.finishOther(this);
        changeTab(R.id.rb_login);
    }

    @Override
    protected void initListener() {
        //切换TAB
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                changeTab(checkedId);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        etPassword.setText("");
    }

    //切换布局
    private void changeTab(int id) {
        if (id == mLayoutId) {
            return;
        }
        switch (id) {
            case R.id.rb_login:
                //切换登录页
                rbLogin.setTextSize(19);
                rbSignUp.setTextSize(16);
                viewLogin.setVisibility(View.VISIBLE);
                viewSignup.setVisibility(View.GONE);
                break;
            case R.id.rb_signup:
                //切换注册页
                rbLogin.setTextSize(16);
                rbSignUp.setTextSize(19);
                viewSignup.setVisibility(View.VISIBLE);
                viewLogin.setVisibility(View.GONE);
                break;
            default:
                break;
        }

        mLayoutId = id;
    }

    /**
     * 登陆
     */
    @OnClick(R.id.tv_login)
    void onLogin() {
        if(loginType==1){
            if (etData(etAccount, etPassword)) {
                mPresenter.login(etAccount.getText().toString(),
                        etPassword.getText().toString(),loginType,loginType==2?etLoginCode.getText().toString():"");
            }
        }else {
            if (etData(etAccount)) {
                if(loginType==2&&etLoginCode.getText().toString().isEmpty()){
                    ToastUtil.showToast("请输入验证码!");
                    return;
                }
                mPresenter.login(etAccount.getText().toString(),
                        etPassword.getText().toString(), loginType, etLoginCode.getText().toString());
            }
        }

    }

    /**
     * 注册
     */
    @OnClick(R.id.tv_signup)
    void onSignUp() {
        if (etData(etSignUpAccount, etSignUpCode, etSignUpPassword1, etSignUpPassword2)) {
            mPresenter.signUp(etSignUpAccount.getText().toString(),
                    etSignUpCode.getText().toString(),
                    etSignUpPassword1.getText().toString(),
                    etSignUpPassword2.getText().toString());
        }
    }

    /**
     * 注册获取验证码
     */
    @OnClick(R.id.tv_signup_getcode)
    void onGetCode() {

        if (etSignUpAccount.getText().toString().isEmpty()) {
            viewAnim(etSignUpAccount);
            ToastUtil.showToast("请输入手机号");
            return;
        }
        DialogUtils dialogUtils=new DialogUtils();
        dialogUtils.showDialog(LoginActivity.this,etSignUpAccount.getText().toString(),"1",mPresenter);
       //mPresenter.getCAPTCHA(etSignUpAccount.getText().toString());
    }
    /**
     * 登录获取验证码
     */
    @OnClick(R.id.tv_login_getcode)
    void onGetLoginCode(){

        if(etAccount.getText().toString().isEmpty()){
            viewAnim(etLoginCode);
            ToastUtil.showToast("请输入手机号");
            return;
        }
        DialogUtils dialogUtils=new DialogUtils();
        dialogUtils.showDialog(LoginActivity.this,etAccount.getText().toString(),"2",mPresenter);

      //mPresenter.getLoginCode(etAccount.getText().toString());
    }

    /**
     * 忘记密码
     */
    @OnClick(R.id.tv_login_forget)
    void onForget() {
        Bundle bundle = new Bundle();
        bundle.putInt(Constant.TYPE, ResetLoginPwdActivity.FORGET_PWD_LAYOUT);
        startActivity(ResetLoginPwdActivity.class, bundle);
    }

    /**
     * 点击用户协议
     */
    @OnClick({R.id.ll_login_agreement, R.id.ll_signup_agreement,R.id.ll_login_regist_agreement,R.id.tv_login_argeement})
    void onAgreement() {
        WebActivity.start(getContext(), UrlEnum.c.getTitle(), null, UrlEnum.c.getType());
    }

    /**
     *
     * @return
     */
    @OnClick(R.id.tv_codelogin_passLogin)
    void onLoginType(){
        loginType=loginType==1?2:1;
        tv_codelogin_passLogin.setText(loginType==1?"验证码登录":"密码登录");
        rl_login_code.setVisibility(loginType==1?View.GONE:View.VISIBLE);
        etPassword.setVisibility(loginType==2?View.GONE:View.VISIBLE);
        tvForget.setVisibility(loginType==1?View.VISIBLE:View.INVISIBLE);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //退出APP
            AcCollector.finishAll();
        }
        return false;
    }


    @Override
    public TextView getCAPTCHAView() {
        return tvCAPTCHA;
    }

    @Override
    public TextView getLoginCode() {
        return tv_login_getcode;
    }
}

